package com.ternnetwork.clustersyn.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.file.CloudFile;
import com.microsoft.azure.storage.file.CloudFileClient;
import com.microsoft.azure.storage.file.CloudFileDirectory;
import com.microsoft.azure.storage.file.CloudFileShare;

public class AzureStorageUtil {
	private static final Logger log = Logger.getLogger(AzureStorageUtil.class);
	
	
	public static void main(String[] args) {
		File directory = new File("");// 参数为空
        String projectRoot;
		try {
			projectRoot = directory.getCanonicalPath().replaceAll("\\\\","/").replaceAll("/bin", "");
			PropertyConfigurator.configure(projectRoot+"/conf/log4j.properties");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		createDirectory("test/r/cms/www/red/xml/");
		//uploadFile("D:/image.txt","xxx/yy","image.txt");
		
		//deleteFile("xxx/yy","image.txt");
	}
	
	
	public static String getStorageConnectionString() throws FileNotFoundException, RuntimeException{
		
		return     
			    "DefaultEndpointsProtocol=http;" +
			    "AccountName="+PropUtils.getPropertyValue("azure.storage.account.name")+";" +
			    "AccountKey="+PropUtils.getPropertyValue("azure.storage.account.key")+";" +
			    "EndpointSuffix=core.Chinacloudapi.cn";
	}
	
	public static void createDirectory(String directory){
		
		try {
			
		CloudStorageAccount storageAccount = CloudStorageAccount.parse(getStorageConnectionString());
		 
		CloudFileClient fileClient = storageAccount.createCloudFileClient();
	
		CloudFileShare share = fileClient.getShareReference(PropUtils.getPropertyValue("azure.storage.share.reference"));
		
		CloudFileDirectory rootDir = share.getRootDirectoryReference();

		CloudFileDirectory cloudDir = rootDir.getDirectoryReference(directory);

		  if(cloudDir.createIfNotExists()) {
			  log.info(directory+" created");
		  }else{
			  log.info(directory+" already exists");
		  }
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void uploadFile(String localFilePath,String desRelativeFilePath,String desFileName){
		
		try {
			
		CloudStorageAccount storageAccount = CloudStorageAccount.parse(getStorageConnectionString());
		 
		CloudFileClient fileClient = storageAccount.createCloudFileClient();
	
		CloudFileShare share = fileClient.getShareReference(PropUtils.getPropertyValue("azure.storage.share.reference"));
		
		CloudFileDirectory rootDir = share.getRootDirectoryReference();

		localFilePath=localFilePath.replaceAll("\\\\","/");
		
		CloudFileDirectory cloudDir = rootDir.getDirectoryReference(desRelativeFilePath);

		CloudFile cloudFile = cloudDir.getFileReference(desFileName);
	
		cloudFile.uploadFromFile(localFilePath);
		 
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void deleteFile(String desRelativeFilePath,String desFileName){
		
		try {
			
		CloudStorageAccount storageAccount = CloudStorageAccount.parse(getStorageConnectionString());
		 
		CloudFileClient fileClient = storageAccount.createCloudFileClient();
	
		CloudFileShare share = fileClient.getShareReference(PropUtils.getPropertyValue("azure.storage.share.reference"));
		
		CloudFileDirectory rootDir = share.getRootDirectoryReference();

		CloudFileDirectory cloudDir = rootDir.getDirectoryReference(desRelativeFilePath);

		CloudFile cloudFile = cloudDir.getFileReference(desFileName);
	
		cloudFile.deleteIfExists();
		 
		}catch(Exception e){
			e.printStackTrace();
		}
	}


}
